package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.vo.ChartXData;
import com.chart.chart.domian.vo.ChartXDataVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 散点图
 *
 * @author chart
 */
@RestController
@RequestMapping("/scatterPoint/chart")
public class ScatterPointChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;


    @PostMapping("/scatter")
    public AjaxResult scatter(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<Object>>> newList = new ArrayList<>();

            List<List<Object>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                if (!CollectionUtils.isEmpty(sdata)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < sdata.size(); i1++) {
                        String s = sdata.get(i1);

                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }

                    if (!CollectionUtils.isEmpty(data)) {
                        if (i == getdata.size() - 1) {
                            List<Object> datas = new ArrayList<>();
                            for (int i1 = 0; i1 < data.size(); i1++) {
                                String s = data.get(i1);
                                datas.add(Double.parseDouble(s));
                            }
                            list.add(datas);
                            newList.add(list);
                        } else if (i == 1) {
                            groupName.add(data.get(0));
                        } else {
                            if (getdata.get(i).size() == 1) {
                                groupName.add(data.get(0));
                                newList.add(list);
                                list = new ArrayList<>();
                            } else {
                                List<Object> datas = new ArrayList<>();
                                for (int i1 = 0; i1 < data.size(); i1++) {
                                    String s = data.get(i1);
                                    datas.add(Double.parseDouble(s));
                                }
                                list.add(datas);
                            }
                        }
                    }
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            ArrayList<Map> mapss = new ArrayList<>();
            for (int i = 0; i < newList.size(); i++) {
                Map map = new HashMap<>();
                map.put("source",newList.get(i));
                mapss.add(map);
            }

            jsonObject.put("dataset", mapss);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(1);
            ArrayList<Map> maps = new ArrayList<>();

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("datasetIndex", i);
                jsonObject2.put("name", groupName.get(i));
                Map<String, String> map = new HashMap<>();
                map.put("name",groupName.get(i));
                maps.add(map);
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            legend.put("data",maps);
            jsonObject.put("legend", legend);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

    //极坐标图
    @PostMapping("/polarCoordinates/importData")
    public AjaxResult polarCoordinates(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName =new ArrayList<>();


            List<List<Double>> groupList = new ArrayList<>();

            List<Map> maps = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> colData = getdata.get(i);
                if (!CollectionUtils.isEmpty(colData)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < colData.size(); i1++) {
                        String s = colData.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }

                    if (data.size()==1) {
                        if (i!=1) {
                            Map map = new HashMap<>();
                            map.put("source",groupList);
                            maps.add(map);
                            groupList = new ArrayList<>();
                        }
                        groupName.addAll(data);
                    }else {
                        List<Double> dataList = new ArrayList<>();
                        dataList.add(Double.parseDouble(data.get(0)));
                        dataList.add(Double.parseDouble(data.get(1)));
                        groupList.add(dataList);
                        if (getdata.size()-1==i) {
                            Map map = new HashMap<>();
                            map.put("source",groupList);
                            maps.add(map);
                        }
                    }
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", maps);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("datasetIndex", i);
                jsonObject2.put("name", groupName.get(i));
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

    //具有不同点大小和色调的散点图
    @PostMapping("/pointSizes/importData")
    public AjaxResult pointSizes(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            if (ObjectUtils.isEmpty(getdata.get(0).get(1))) {
                return AjaxResult.error("请填充最大值数据");
            }
            String minV = getdata.get(0).get(1);
            String maxV = getdata.get(1).get(1);

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();
            List<List<String>> dataList = new ArrayList<>();
            for (int i = 2; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    if (!CollectionUtils.isEmpty(newList)) {
                        if (i==2) {
                            newList.remove(0);
                            xList.addAll(newList);
                        }else {
                            String y = newList.get(0);
                            yList.add(y);
                            newList.remove(0);
                            dataList.add(newList);
                        }
                    }
                }
            }


            List<List<Object>> newDataList = new ArrayList<>();
            List<Object> biao = new ArrayList<>();
            biao.add("x");
            biao.add("y");
            biao.add("z");
            newDataList.add(biao);
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                for (int j = 0; j < xList.size(); j++) {
                    String s = dataList.get(i).get(j);
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));
                    newDataList.add(cellList);
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("source",newDataList);

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            List<JSONObject> visualMap = (List<JSONObject>) jsonObject.get("visualMap");
            for (int i = 0; i < visualMap.size(); i++) {
                JSONObject jsonObject1 = visualMap.get(i);
                jsonObject1.put("min",Double.parseDouble(minV));
                jsonObject1.put("max",Double.parseDouble(maxV));
            }
            jsonObject.put("visualMap", visualMap);

            jsonObject.put("dataset", map);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //带有分类变量的散点图
    @PostMapping("/scaWithCatVariables/importData")
    public AjaxResult scaWithCatVariables(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<String>>> newList = new ArrayList<>();

            List<List<String>> list = new ArrayList<>();

            ArrayList<Object> xilieName = new ArrayList<>();

            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                List<String> list1 = new ArrayList<>();
                for (int i1 = 0; i1 < sdata.size(); i1++) {
                    String s = sdata.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        list1.add(s);
                    }
                }
                if (!CollectionUtils.isEmpty(list1)) {
                    if (i == getdata.size() - 1) {
                        if (!CollectionUtils.isEmpty(list1)) {
                            String s = list1.get(0);
                            xilieName.add(s);
                            list1.remove(0);
                            list.add(list1);
                        }
                        newList.add(list);
                    } else if (i == 1) {
                        groupName.add(list1.get(0));
                    } else {
                        if (getdata.get(i).size() == 1) {
                            groupName.add(list1.get(0));
                            newList.add(list);
                            list = new ArrayList<>();
                        } else {
                            String s = list1.get(0);
                            xilieName.add(s);
                            list1.remove(0);
                            list.add(list1);
                        }
                    }
                }
            }


            List<Map> maps = new ArrayList<>();
            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            for (int i = 0; i < newList.size(); i++) {
                List<List<String>> lists = newList.get(i);
                for (int i1 = 0; i1 < lists.size(); i1++) {
                    List<List<Integer>> mint = new ArrayList<>();
                    List<String> strings = lists.get(i1);
                    for (int i2 = 0; i2 < strings.size(); i2++) {
                        String s = strings.get(i2);
                        List<Integer> xint = new ArrayList<>();
                        if (Double.parseDouble(s) > 0) {
                            xint.add(i1);
                            xint.add(i2);
                            mint.add(xint);
                        }
                    }
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("source", mint);
                    maps.add(map);
                }
            }

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();

            List<List<String>> lists = newList.get(0);
            for (int i = 0; i < maps.size(); i++) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.putAll(json);
                jsonObject1.put("name", xilieName.get(i));

                if (i >= lists.size()) {
                    jsonObject1.put("xAxisIndex", 1);
                    jsonObject1.put("yAxisIndex", 1);
                } else {
                    jsonObject1.put("xAxisIndex", 0);
                    jsonObject1.put("yAxisIndex", 0);
                }
                jsonObject1.put("datasetIndex", i);
                jsonObjects.add(jsonObject1);
            }


            jsonObject.put("series", jsonObjects);

            jsonObject.put("dataset", maps);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

    //多元线性回归图
    @PostMapping("/multLinRegression/importData")
    public AjaxResult multLinRegression(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<String>>> newList = new ArrayList<>();

            List<List<String>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                if (!CollectionUtils.isEmpty(sdata)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < sdata.size(); i1++) {
                        String s = sdata.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }

                    if (!CollectionUtils.isEmpty(data)) {
                        if (i == getdata.size() - 1) {
                            list.add(data);
                            newList.add(list);
                        } else if (i == 1) {
                            groupName.add(data.get(0));
                        } else {
                            if (getdata.get(i).size() == 1) {
                                groupName.add(data.get(0));
                                newList.add(list);
                                list = new ArrayList<>();
                            } else {
                                list.add(data);
                            }
                        }
                    }
                }
            }

            List<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();


            for (int i = 0; i < newList.size(); i++) {
                List<List<String>> lists = newList.get(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);

                double[] x = new double[lists.size()];
                double[] y = new double[lists.size()];

                for (int i1 = 0; i1 < lists.size(); i1++) {
                    List<String> sources = lists.get(i1);
                    for (int i2 = 0; i2 < sources.size(); i2++) {
                        x[i1] = Double.parseDouble(sources.get(0));
                        y[i1] = Double.parseDouble(sources.get(1));
                    }
                }
                Map service = ExcelDataUtils.getService1(x, y);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.putAll(json);
                jsonObject1.put("datasetIndex", i);
                jsonObject1.put("name", groupName.get(i));
                JSONObject markLine1 = (JSONObject) jsonObject1.get("markLine");
                JSONObject markLine = new JSONObject();
                markLine.putAll(markLine1);
                JSONObject label1 = (JSONObject) markLine.get("label");
                JSONObject label = new JSONObject();
                label.putAll(label1);
                label.put("formatter", service.get("formatter"));
                markLine.put("label", label);

                JSONObject tooltip1 = (JSONObject) markLine.get("tooltip");
                JSONObject tooltip = new JSONObject();
                tooltip.putAll(tooltip1);
                tooltip.put("formatter", service.get("formatter"));
                markLine.put("tooltip", tooltip);

                List<JSONObject> data = (List<JSONObject>) markLine.get("data");
                List<JSONObject> data1 = (List<JSONObject>) data.get(0);
                List<JSONObject> jsonObjects1 = new ArrayList<>();
                List<List<JSONObject>> jsonObjects2 = new ArrayList<>();
                for (int i1 = 0; i1 < data1.size(); i1++) {
                    JSONObject jsonObject2 = data1.get(i1);
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.putAll(jsonObject2);
                    if (i1 == 0) {
                        jsonObject3.put("coord", service.get("min"));
                    } else {
                        jsonObject3.put("coord", service.get("max"));
                    }
                    jsonObjects1.add(jsonObject3);
                }
                jsonObjects2.add(jsonObjects1);
                markLine.put("data", jsonObjects2);
                jsonObject1.put("markLine", markLine);
                jsonObjects.add(jsonObject1);
            }

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            ArrayList<Map> maps1 = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                Map map = new HashMap<>();
                map.put("name",groupName.get(i));
                maps1.add(map);
            }

            legend.put("data", maps1);
            jsonObject.put("legend", legend);


            jsonObject.put("series", jsonObjects);
            jsonObject.put("dataset", maps);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //绘图模型残差图
    @PostMapping("/scatterResiduals/importData")
    public AjaxResult scatterResiduals(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<String>>> newList = new ArrayList<>();

            List<List<String>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                List<String> sdata = getdata.get(i);
                if (!CollectionUtils.isEmpty(sdata)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < sdata.size(); i1++) {
                        String s = sdata.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }
                    if (!CollectionUtils.isEmpty(data)) {
                        if (i == getdata.size() - 1) {
                            list.add(data);
                            newList.add(list);
                        } else if (i == 1) {
                            groupName.add(data.get(0));
                        } else {
                            if (getdata.get(i).size() == 1) {
                                groupName.add(data.get(0));
                                newList.add(list);
                                list = new ArrayList<>();
                            } else {
                                list.add(data);
                            }
                        }
                    }
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            List<List<String>> lists = newList.get(0);

            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject1 = series.get(i);

                if (i == 0) {
                    lists = newList.get(i);
                    jsonObject1.put("data", lists);
                } else {
                    double[] x = new double[lists.size()];
                    double[] y = new double[lists.size()];
                    for (int i1 = 0; i1 < lists.size(); i1++) {
                        List<String> sources = lists.get(i1);
                        for (int i2 = 0; i2 < sources.size(); i2++) {
                            x[i1] = Double.parseDouble(sources.get(0));
                            y[i1] = Double.parseDouble(sources.get(1));
                        }
                    }
                    //求出每个点对应的方差
                    List<List<String>> service = ExcelDataUtils.getService2(x, y);

                    jsonObject1.put("data", service);
                }
                jsonObjects.add(jsonObject1);
            }


            jsonObject.put("series", jsonObjects);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //一元线性回归图
    @PostMapping("/univariateLinRegression/importData")
    public AjaxResult univariateLinRegression(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();

            List<List<List<String>>> newList = new ArrayList<>();

            List<List<String>> list = new ArrayList<>();


            for (int i = 1; i < getdata.size(); i++) {
                if (i == getdata.size() - 1) {
                    List<String> sdata = getdata.get(i);
                    list.add(sdata);
                    newList.add(list);
                } else if (i == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                } else {
                    if (getdata.get(i).size() == 1) {
                        List<String> gName = getdata.get(i);
                        groupName.add(gName.get(0));
                        newList.add(list);
                        list = new ArrayList<>();
                    } else {
                        List<String> sdata = getdata.get(i);
                        ArrayList<String> newList1 = new ArrayList<>();
                        for (int i1 = 0; i1 < sdata.size(); i1++) {
                            String s = sdata.get(i1);
                            if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                                newList1.add(s);
                            }
                        }
                        list.add(newList1);
                    }
                }
            }

            ArrayList<Map> maps = new ArrayList<>();

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();


            for (int i = 0; i < newList.size(); i++) {
                List<List<String>> lists = newList.get(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("source", lists);
                maps.add(map);

                double[] x = new double[lists.size()];
                double[] y = new double[lists.size()];

                for (int i1 = 0; i1 < lists.size(); i1++) {
                    List<String> sources = lists.get(i1);
                    for (int i2 = 0; i2 < sources.size(); i2++) {
                        x[i1] = Double.parseDouble(sources.get(0));
                        y[i1] = Double.parseDouble(sources.get(1));
                    }
                }
                Map service = ExcelDataUtils.getService1(x, y);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.putAll(json);
                jsonObject1.put("datasetIndex", i);
                jsonObject1.put("name", groupName.get(i));
                JSONObject markLine1 = (JSONObject) jsonObject1.get("markLine");
                JSONObject markLine = new JSONObject();
                markLine.putAll(markLine1);
                JSONObject label1 = (JSONObject) markLine.get("label");
                JSONObject label = new JSONObject();
                label.putAll(label1);
                label.put("formatter", service.get("formatter"));
                markLine.put("label", label);

                JSONObject tooltip1 = (JSONObject) markLine.get("tooltip");
                JSONObject tooltip = new JSONObject();
                tooltip.putAll(tooltip1);
                tooltip.put("formatter", service.get("formatter"));
                markLine.put("tooltip", tooltip);

                List<JSONObject> data = (List<JSONObject>) markLine.get("data");
                List<JSONObject> data1 = (List<JSONObject>) data.get(0);
                List<JSONObject> jsonObjects1 = new ArrayList<>();
                List<List<JSONObject>> jsonObjects2 = new ArrayList<>();
                for (int i1 = 0; i1 < data1.size(); i1++) {
                    JSONObject jsonObject2 = data1.get(i1);
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.putAll(jsonObject2);
                    if (i1 == 0) {
                        jsonObject3.put("coord", service.get("min"));
                    } else {
                        jsonObject3.put("coord", service.get("max"));
                    }
                    jsonObjects1.add(jsonObject3);
                }
                jsonObjects2.add(jsonObjects1);
                markLine.put("data", jsonObjects2);
                jsonObject1.put("markLine", markLine);
                jsonObjects.add(jsonObject1);
            }


            JSONObject legend = (JSONObject) jsonObject.get("legend");
            ArrayList<Map> maps1 = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                Map map = new HashMap<>();
                map.put("name",groupName.get(i));
                maps1.add(map);
            }

            legend.put("data", maps1);
            jsonObject.put("legend", legend);

            jsonObject.put("series", jsonObjects);
            jsonObject.put("dataset", maps);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

}
